package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Featurestandard;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("FeaturestandardDAO")
@Transactional
public class FeaturestandardDAOImpl extends AbstractJpaDao<Featurestandard> implements FeaturestandardDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Featurestandard.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FeaturestandardDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Featurestandard findFeaturestandardById(Integer id) throws DataAccessException {

		return findFeaturestandardById(id, -1, -1);
	}

	@Transactional
	public Featurestandard findFeaturestandardById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFeaturestandardById", startResult, maxRows, id);
			return (Featurestandard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Featurestandard> findFeaturestandardByName(String name) throws DataAccessException {

		return findFeaturestandardByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Featurestandard> findFeaturestandardByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFeaturestandardByName", startResult, maxRows, name);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@Transactional
	public Set<Featurestandard> findFeaturestandardByNameContaining(String name) throws DataAccessException {

		return findFeaturestandardByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Featurestandard> findFeaturestandardByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFeaturestandardByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@Transactional
	public Set<Featurestandard> findFeaturestandardByMax(BigDecimal max) throws DataAccessException {

		return findFeaturestandardByMax(max, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Featurestandard> findFeaturestandardByMax(BigDecimal max, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFeaturestandardByMax", startResult, maxRows, max);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@Transactional
	public Set<Featurestandard> findFeaturestandardByMin(BigDecimal min) throws DataAccessException {

		return findFeaturestandardByMin(min, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Featurestandard> findFeaturestandardByMin(BigDecimal min, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFeaturestandardByMin", startResult, maxRows, min);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@Transactional
	public Set<Featurestandard> findFeaturestandardByDescription(String description) throws DataAccessException {

		return findFeaturestandardByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Featurestandard> findFeaturestandardByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFeaturestandardByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@Transactional
	public Set<Featurestandard> findFeaturestandardByDescriptionContaining(String description)
			throws DataAccessException {

		return findFeaturestandardByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Featurestandard> findFeaturestandardByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFeaturestandardByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@Transactional
	public Featurestandard findFeaturestandardByPrimaryKey(Integer id) throws DataAccessException {
		return findFeaturestandardByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Featurestandard findFeaturestandardByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFeaturestandardByPrimaryKey", startResult, maxRows, id);
			return (Featurestandard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Featurestandard> findFeaturestandardsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFeaturestandardsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Featurestandard> findAllFeaturestandards() throws DataAccessException {

		return findAllFeaturestandards(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Featurestandard> findAllFeaturestandards(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFeaturestandards", startResult, maxRows);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Featurestandard> findFeaturestandardsByFeature(Integer featureId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFeaturestandardsByFeature", start, max, featureId);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Featurestandard> findFeaturestandardsByStandard(Integer standardId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFeaturestandardsByStandard", start, max, standardId);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Featurestandard> findFeaturestandardsByUnit(Integer unitId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFeaturestandardsByUnit", start, max, unitId);
		return new LinkedHashSet<Featurestandard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Featurestandard> findFeaturestandardByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Featurestandard>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Featurestandard> findFeaturestandardsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Featurestandard.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Featurestandard> featurestandards = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return featurestandards;
	}

	@Transactional
	public Featurestandard findFeaturestandardByPhysicalPrimaryKey(Integer unitId, Integer standardId,
			Integer featureId) throws DataAccessException {
		return findFeaturestandardByPhysicalPrimaryKey(unitId, standardId, featureId, -1, -1);
	}

	@Transactional
	public Featurestandard findFeaturestandardByPhysicalPrimaryKey(Integer unitId, Integer standardId,
			Integer featureId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFeaturestandardByPhyscialPrimaryKey", startResult, maxRows, unitId,
					standardId, featureId);
			return (Featurestandard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Featurestandard entity) {
		return true;
	}
}
